package other;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import utils.TreeNode;

public class averageOfLevels_637 {
    public List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<Double> list = new ArrayList<>();
        while (!queue.isEmpty()) {
            //层序遍历
            int size = queue.size();
            double sum = 0;
            for (int i=0;i<size;i++){    //遍历一层
                TreeNode node = queue.poll();
                sum += node.val;
                if(node.left!=null)queue.add(node.left);
                if(node.right!=null)queue.add(node.right);
            }
            list.add(sum/size);
        }
        return list;
    }
}
